You can provide an anonymous function in any situation where you might have used a
lambda expression. However, while you can have a trailing parameter's lambda expression
be outside of the function call (e.g., events.first { it.id == 1337 }
), anonymous
functions have to passed inside the function call, as would ordinary parameters.
You can learn more about this in:
Tags:
xxxxxxxxxx
1
data class Event(val id: Int)
2
3
fun main() {
4
val events = listOf(Event(1), Event(5), Event(1337), Event(24601), Event(42), Event(-6))
5
6
val leetEvent = events.first(fun(it: Event): Boolean { return it.id == 1337 })
7
8
println(leetEvent)
9
}
10